import React, { Component } from 'react'
import Resource from '@/components/Resource';
import { Row, Col, Table, message, Button, Form, Input, Radio } from 'antd';
import { connect } from 'dva';

interface Props {
    value: string, // PDF url
    dispatch: Function,
    onChange: Function,
}

@connect()
export class UploadLink extends Component<Props> {

    uploadFile(e) {
        e.persist();
        const { dispatch, onChange } = this.props;
        const targetFile = e.target.files[0];

        
        dispatch({
            type: 'upload/uploadFile',
            payload: targetFile
        }).then(() => {
            //TODO 获取文件 URL。 预备做文件预览
            onChange('Uploaded pdf file url');
            // if (targetFile.type === 'application/pdf') {
            //     file.message === 'success' && this.setState({ fileName: targetFile.name })
            // } else {
            //     image.message === 'success' && this.setState({ fileName: targetFile.name })
            // }
        })
    }

    render() {
        const {
            value,
            onChange
        } = this.props;
        return (
            <>
                {value ? <Resource fileName={"审批文件"} 
                    showDelBtn
                    onClick={() => {
                    this.setState({ fileName: null });
                    onChange && onChange(null);
                }
                } /> :
                    <>
                        <input style={{ opacity: 0, width: 150, position: 'absolute', zIndex: 10, cursor: 'pointer' }}
                            onChange={(e) => {
                                
                                this.uploadFile(e)
                            }}
                            onClick={(event) => { 
                                event.target.value = null;
                             }}
                            type='file'
                        />
                        <Button type="link" icon="plus"
                            onClick={(event) => { 

                             }}
                        >上传消防安全责任书</Button>
                    </>
                }
            </>
        )
    }
}

export default UploadLink
